package cn.cms.model;

import cn.cms.util.CronExpressionUtils;

import java.util.Date;

public class ScheduleJob<T> {

    public static final String GO_ONLINE = "goonline";//上线
    public static final String GO_OFFLINE = "gooffline";//下线

    private String jobId;
    private String jobName;
    private String jobGroup;
    private String jobStatus;
    private String goOnLineCronExpression;
    private String goOffLineCronExpression;
    private String operType; // 操作类型，上线还是下线

    private Integer scheduleType;
    private Date scheduleBeginTime;
    private Date scheduleEndTime;
    private Integer scheduleRepeatType;
    private Date scheduleRepeatBeginTime;
    private Date scheduleRepeatEndTime;
    private Integer scheduleDuration;
    private String scheduleRepeatWeekPattern;
    private String scheduleRepeatMonthPattern;

    private Class jobClazz;//执行具体操作的Class（manager操作target对象）
    private T taraget;//目标对象
    private T manager;//管理目标对象的mananger

    public String getJobId() {
        return jobId;
    }

    public void setJobId(String jobId) {
        this.jobId = jobId;
    }

    public String getJobName() {
        return jobName;
    }

    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    public String getJobGroup() {
        return jobGroup;
    }

    public void setJobGroup(String jobGroup) {
        this.jobGroup = jobGroup;
    }

    public String getJobStatus() {
        return jobStatus;
    }

    public void setJobStatus(String jobStatus) {
        this.jobStatus = jobStatus;
    }

    public String getGoOnLineCronExpression() {
        return goOnLineCronExpression;
    }

    public void setGoOnLineCronExpression(String goOnLineCronExpression) {
        this.goOnLineCronExpression = goOnLineCronExpression;
    }

    public String getGoOffLineCronExpression() {
        return goOffLineCronExpression;
    }

    public void setGoOffLineCronExpression(String goOffLineCronExpression) {
        this.goOffLineCronExpression = goOffLineCronExpression;
    }

    public String getOperType() {
        return operType;
    }

    public void setOperType(String operType) {
        this.operType = operType;
    }

    public Integer getScheduleType() {
        return scheduleType;
    }

    public void setScheduleType(Integer scheduleType) {
        this.scheduleType = scheduleType;
    }

    public Date getScheduleBeginTime() {
        return scheduleBeginTime;
    }

    public void setScheduleBeginTime(Date scheduleBeginTime) {
        this.scheduleBeginTime = scheduleBeginTime;
    }

    public Date getScheduleEndTime() {
        return scheduleEndTime;
    }

    public void setScheduleEndTime(Date scheduleEndTime) {
        this.scheduleEndTime = scheduleEndTime;
    }

    public Date getScheduleRepeatBeginTime() {
        return scheduleRepeatBeginTime;
    }

    public void setScheduleRepeatBeginTime(Date scheduleRepeatBeginTime) {
        this.scheduleRepeatBeginTime = scheduleRepeatBeginTime;
    }

    public Date getScheduleRepeatEndTime() {
        return scheduleRepeatEndTime;
    }

    public void setScheduleRepeatEndTime(Date scheduleRepeatEndTime) {
        this.scheduleRepeatEndTime = scheduleRepeatEndTime;
    }

    public Integer getScheduleDuration() {
        return scheduleDuration;
    }

    public void setScheduleDuration(Integer scheduleDuration) {
        this.scheduleDuration = scheduleDuration;
    }

    public Integer getScheduleRepeatType() {
        return scheduleRepeatType;
    }

    public void setScheduleRepeatType(Integer scheduleRepeatType) {
        this.scheduleRepeatType = scheduleRepeatType;
    }

    public String getScheduleRepeatWeekPattern() {
        return scheduleRepeatWeekPattern;
    }

    public void setScheduleRepeatWeekPattern(String scheduleRepeatWeekPattern) {
        this.scheduleRepeatWeekPattern = scheduleRepeatWeekPattern;
    }

    public String getScheduleRepeatMonthPattern() {
        return scheduleRepeatMonthPattern;
    }

    public void setScheduleRepeatMonthPattern(String scheduleRepeatMonthPattern) {
        this.scheduleRepeatMonthPattern = scheduleRepeatMonthPattern;
    }

    public Class getJobClazz() {
        return jobClazz;
    }

    public void setJobClazz(Class jobClazz) {
        this.jobClazz = jobClazz;
    }

    public T getTaraget() {
        return taraget;
    }

    public void setTaraget(T taraget) {
        this.taraget = taraget;
    }

    public T getManager() {
        return manager;
    }

    public void setManager(T manager) {
        this.manager = manager;
    }

    //刷新，根据相关信息得到cron表达式
    public void refresh()
    {
        if (this.getScheduleType() == 0) {//固定
            this.goOnLineCronExpression = CronExpressionUtils.getCronExpressionFromFormatDate(this.getScheduleBeginTime());
            this.goOffLineCronExpression = CronExpressionUtils.getCronExpressionFromFormatDate(this.getScheduleEndTime());
        }
        if (this.getScheduleType() == 1) {//重复
//            Calendar cal = Calendar.getInstance();
//            cal.setTime(this.getScheduleRepeatBeginTime());
//            cal.add(Calendar.MINUTE, this.getScheduleDuration());
//            Date repeatEndTime = cal.getTime();
            Date repeatEndTime = this.getScheduleRepeatEndTime();
            if (this.getScheduleRepeatType() == 0) {//每天
                this.goOnLineCronExpression = CronExpressionUtils.getDailyCronExpressionFromDate(this.getScheduleRepeatBeginTime());
                this.goOffLineCronExpression = CronExpressionUtils.getDailyCronExpressionFromDate(repeatEndTime);
            }
            if (this.getScheduleRepeatType() == 1) {//每周
                this.goOnLineCronExpression = CronExpressionUtils.getWeeklyCronExpressionFromDate(this.getScheduleRepeatBeginTime(), this.getScheduleRepeatWeekPattern());
                this.goOffLineCronExpression = CronExpressionUtils.getWeeklyCronExpressionFromDate(repeatEndTime, this.getScheduleRepeatWeekPattern());
            }
            if (this.getScheduleRepeatType() == 2) {//每月
                this.goOnLineCronExpression = CronExpressionUtils.getMonthlyCronExpressionFromDate(this.getScheduleRepeatBeginTime(), this.getScheduleRepeatMonthPattern());
                this.goOffLineCronExpression = CronExpressionUtils.getMonthlyCronExpressionFromDate(repeatEndTime, this.getScheduleRepeatMonthPattern());
            }
        }
    }
}
